const app = getApp();
const Url = app.globalData.Url
Page({
  data: {
    expreIndex: '',
    entering: '智能录入',
    enteringFalg: true,
    capacity: true,
    tradition: false,
    guanbi: true,
    recipientsName: '',
    recipientsPhone: '',
    receiverRegion: ['省份..', '城市..', '区县..'],
    recipientsSite: '',
    infomation: '',
    senderNmae: '',
    senderPhone: '',
    senderSite: '',
    goodsIndex: '',
    remak: ''
  },
  onLoad: function() {
    let that = this;
    let UserID = "201701031229AA";
    let ShopID = "HBWH0002";
    //获取本店铺支持电子面单的快递公司列表
    wx.request({
      url: Url + '/dianjia/expressenhance/getEorderExpListInfo',
      data: {
        ShopID: ShopID
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        let result = res.data.result;
        if (result === 1) {
          let expressCompany = res.data.ExpressCorpName;
          let expreArray = ['中通快递', '圆通快递', '韵达快递', '申通快递', '百世汇通'];
          let expreIndex = 0;
          for (let i = 0; i < expreArray.length; i++) {
            if (expreArray[i] == expressCompany) {
              expreIndex = i;
            }
          }
          that.setData({
            expreIndex: expreIndex
          })
        }
      },
      fail: () => {
        console.log("调用失败");
      },
      complete: () => {
        // complete
      }
    })
    //获取寄件人信息
    wx.request({
      url: Url + '/dianjia/expressenhance/getUserDefAddr',
      data: {
        UserID: UserID
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        let senderNmae = res.data.AddressName;
        let senderPhone = res.data.AddressMobile;
        let senderSite = res.data.AddressDescription;
        that.setData({
          senderNmae: senderNmae,
          senderPhone: senderPhone,
          senderSite: senderSite,
        })
      },
      fail: () => {
        console.log("调用失败");
      },
      complete: () => {
        //调用完后出发
      }
    })
  },
  Back: () => {
    wx.navigateBack({
      delta: 1
    })
  },
  history: () => {
    wx.navigateTo({
      url: 'history'
    })
  },
  onCompany: function(e) {
    this.setData({
      expreIndex: e.detail.expreIndex
    })
  },
  // voice: () => {

  // },
  camera: options => {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
        let base64 = wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], 'base64')
        console.log(base64)
        wx.request({
          url: Ulr + '/dianjia/expressenhance/picAddrPhase',
          data: {
            picPhaser: base64
          },
          method: 'POST',
          header: {
            'content-type': 'application/josn'
          },
          success: res => {
            console.log(res)
          }
        })
      },
      fail: res => {
        console.log(res.errMsg)
      },
      complete: () => {

      }
    })
  },
  Entering: function() {
    if (this.data.enteringFalg) {
      this.setData({
        entering: '传统录入',
        enteringFalg: false,
        capacity: false,
        tradition: true
      })
    } else {
      this.setData({
        entering: '智能录入',
        enteringFalg: true,
        capacity: true,
        tradition: false
      })
    }
  },
  discern: function() {
    let info = this.data.infomation
    wx.request({
      url: Url + '/dianjia/expressenhance/textAddrPhase',
      data: {
        textPhaser: info
      },
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        console.log(res)
        let Province = res.data.Province;
        let CityName = res.data.CityName;
        let AreaName = res.data.AreaName;
        let newAddr = [Province, CityName, AreaName];
        let Receiver_Name = res.data.RealName;
        let Receiver_Mobile = res.data.Mobile;
        let Receiver_Address = res.data.AddressDetail;
        this.setData({
          recipientsName: Receiver_Name,
          recipientsPhone: Receiver_Mobile,
          recipientsSite: Receiver_Address,
          receiverRegion: newAddr
        })
        this.Entering()
      },
      fail: err => {
        console.log(err)
        console.log("调用失败");
      },
      complete: function() {
        // complete
        wx.showModal({
          title: '提醒',
          content: '请仔细核对识别的信息，如果误差请手动修改',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },
  relation: () => {
    wx.navigateTo({
      url: 'relation'
    })
  },
  getRecipientsName: function(e) {
    this.setData({
      recipientsName: e.detail.value
    })
  },
  getRecipientsPhone: function(e) {
    this.setData({
      recipientsPhone: e.detail.value
    })
  },
  getRecipientsSite: function(e) {
    this.setData({
      recipientsSite: e.detail.value
    })
  },
  getInfomation: function(e) {
    this.setData({
      infomation: e.detail.value
    })
  },
  getSenderName: function(e) {
    this.setData({
      senderNmae: e.detail.value
    })
  },
  getSenderPhone: function(e) {
    this.setData({
      senderPhone: e.detail.value
    })
  },
  getSenderSite: function(e) {
    this.setData({
      senderSite: e.detail.value
    })
  },
  onSite: function(e) {
    this.setData({
      receiverRegion: e.detail.receiverRegion,
    })
  },
  onDescribe: function(e) {
    this.setData({
      goodsIndex: e.detail.goodsIndex
    })
  },
  getRemak: function(e) {
    this.setData({
      remak: e.detail.value
    })
  },
  submit: function() {
    let params = {
      ShopID: "HBWH0002",
      UserID: "201701031229AA",
      appCustomerID: "dianjiaC001",
      expressCompany: this.data.expreIndex,
      recipientsName: this.data.recipientsName,
      recipientsPhone: this.data.recipientsPhone,
      recipientsSite: this.data.recipientsSite,
      receiverProvinceName: this.data.receiverRegion[0],
      receiverCityName: this.data.receiverRegion[1],
      receiverExpAreaName: this.data.receiverRegion[2],
      senderNmae: this.data.senderNmae,
      senderPhone: this.data.senderPhone,
      senderSite: this.data.senderSite,
      goodsName: this.data.goodsIndex,
      remak: this.data.remak
    }
    //判断填写信息
    if (!params.expressCompany || params.expressCompany == '请选择') {
      wx.showToast({
        title: '请选择快递公司！',
        icon: 'none',
        duration: 2000
      });
    } else
    if (!params.recipientsName || !params.recipientsPhone || !params.recipientsSite || !params.receiverProvinceName || params.receiverProvinceName == '省份..' || !params.receiverCityName || params.receiverCityName == '城市..' || !params.receiverExpAreaName || params.receiverExpAreaName == "区县..") {
      wx.showToast({
        title: '请正确填写收件人信息！',
        icon: 'none',
        duration: 2000
      });
    } else
    if (!params.senderNmae || !params.senderPhone || !params.senderSite) {
      wx.showToast({
        title: '请正确填写寄件人信息！',
        icon: 'none',
        duration: 2000
      });
    } else
    if (!params.goodsName || !params.remak || params.goodsName == '请选择..') {
      wx.showToast({
        title: '请正确填写物品信息！',
        icon: 'none',
        duration: 2000
      });
    } else {
      wx.showActionSheet({
        itemList: ['自己送驿站', '上门收快递'],
        success: res => {
          let expFlag;
          res.tapIndex === 0 ? expFlag = 1 : expFlag = 2;
          let expressInfo = {
            params: params,
            expFlag: expFlag
          }
          wx.setStorageSync('expressInfo', expressInfo)
          wx.navigateTo({
            url: 'aff',
          })
        },
        fail: err => {
          console.log(err)
        }
      })
    }
  }
})